package com.sky.directdeal.activity;

import java.util.List;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.sky.directdeal.R;
import com.sky.directdeal.adapter.BusinessOfferListAdapter;
import com.sky.directdeal.constants.Constants;
import com.sky.directdeal.listener.ServiceResponseListener;
import com.sky.directdeal.manager.ServiceManager;
import com.sky.local_ads.store_list.StoreDetails;
import com.sky.local_ads.store_offers.StoreOfferResponse;
import com.sky.local_ads.store_offers.StoreOffersList;

public class StoreOffersListActivity extends BaseActivity {
	private ListView offersListView;
	private BusinessOfferListAdapter businessOfferListAdapter;
	private boolean isService=false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_business_offers);
		setHomeUp();
		offersListView=(ListView)findViewById(R.id.businessOffersList);
		getBundleData(this);
		
	}
	
	private void getBundleData(Context context) {
		if (getIntent().getExtras() != null
				&& getIntent().hasExtra(Constants.BUSINESS_DETAILS_INTENT)) {
			StoreDetails storeDetails = (StoreDetails) getIntent()
					.getSerializableExtra(Constants.BUSINESS_DETAILS_INTENT);
			if (storeDetails != null) {
				setHeaderTitle(storeDetails.getBusinessName()+" "+getString(R.string.offers));
				String businessId = storeDetails.getBusinessId();
				if (!isService) {
					isService = true;
					mainCategoryHandler.sendEmptyMessage(Constants.SHOW_PROGRESS_DIALOG);
					ServiceManager.getStoreOffers(context, businessId,
							offersResponseListener);
				}
			}
		}
	}

private ServiceResponseListener<StoreOfferResponse> offersResponseListener=new ServiceResponseListener<StoreOfferResponse>() {
		
		@Override
		public void onSuccess(StoreOfferResponse response) {
			mainCategoryHandler.sendEmptyMessage(Constants.DISMISS_PROGRESS_DIALOG);
			isService=false;
			
			if(response!=null && response.getStoreOffersList()!=null){
				
				if( response.getStoreOffersList().size()>0){
					List<StoreOffersList> offerList=response.getStoreOffersList();
					businessOfferListAdapter=new BusinessOfferListAdapter(StoreOffersListActivity.this, offerList);
					offersListView.setAdapter(businessOfferListAdapter);
					
				}else{
					showAlert(StoreOffersListActivity.this, getString(R.string.alert), getString(R.string.no_offers_found), getString(R.string.ok_button), positiveListener, null, null);
				}
				
			}else{
				showAlert(StoreOffersListActivity.this, getString(R.string.alert), getString(R.string.no_offers_found), getString(R.string.ok_button), positiveListener, null, null);
			}
		}
		
		@Override
		public void onFailure(Throwable throwable, String errorResponse) {
			mainCategoryHandler.sendEmptyMessage(Constants.DISMISS_PROGRESS_DIALOG);
			isService=false;
			showAlert(StoreOffersListActivity.this, getString(R.string.server_failed), getString(R.string.server_unavailable), getString(R.string.ok_button), positiveListener, null, null);
			
		}
	};
	
	 public DialogInterface.OnClickListener positiveListener=new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.store_offers, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
